﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Giraffe.Client.Common.Managers;
using Giraffe.Client.Common.WindowsController;
using Giraffe.Core.Common;
using Giraffe.Core.Common.Messengers;
using Giraffe.Core.Model;
using Giraffe.Client.Common.Constants;
using Microsoft.Practices.Composite.Presentation.Regions;
using Microsoft.Practices.Composite.Regions;

namespace Giraffe.Client.ConversationModule
{
    public class ConversationManagerPresentation : IConversationManagerPresentation
    {
        private readonly IUser _user;

        public ConversationManagerPresentation(IUser user)
        {
            _user = user;
            _user.ConversationManager.NewConversationRecieved += OnNewConversationRequest;
        }

        public void StartNewConversation(IContact companion, MessengerType messenger)
        {
            StartNewConversation(companion, messenger, null);
        }

        private void StartNewConversation(IContact companion, MessengerType messenger, IConversation conversation)
        {
            string windowId = GetConverationWindowID(companion, messenger);
            IWindowsController wndController = ObjectProvider.Resolve<IWindowsController>();

            if (!wndController.GetIsWindowPresent(windowId))
            {
                IUser user = ObjectProvider.Resolve<IUser>();
                if (conversation == null)
                {
                    conversation = user.ConversationManager.StartConversation(companion);
                }
                
                IRegionManager mainRegionManager = ObjectProvider.Resolve<IRegionManager>();
                IRegionManager scopedRegionManager = mainRegionManager.CreateRegionManager();

                var convVM = ObjectProvider.Resolve<ConversationVM>(
                    new Tuple<string, object>("conversation", conversation),
                    new Tuple<string, object>("regionManager", scopedRegionManager));
                ////var convVM = new ConversationVM(conv, scopedRegionManager);

                IWindowSettings wndSettings = wndController.CreateWindow(windowId, convVM);

                //todo Get default values from settings.
                wndSettings.Height = 400;
                wndSettings.Width = 250;
                wndSettings.Title = companion.Name;
                //todo: use LINQ to resolve EventName
                wndSettings.AddHandler("Closed", new EventHandler((s,a)=>convVM.CloseCommand.Execute(null)));

                wndController.SetRegionManager(windowId, scopedRegionManager);
                wndController.ShowWindow(windowId, false);
                
                convVM.Init();
            }
            else
            {
                wndController.ShowWindow(windowId, false);

            }
        }

        private void OnNewConversationRequest(IConversation conversation)
        {
            StartNewConversation(conversation.Companion, conversation.CurrentMessenger.MessengerType, conversation);
        }
        
        public void CloseConversation(IConversation conversation)
        {
            conversation.Close();

            string windowId = GetConverationWindowID(conversation.Companion, conversation.CurrentMessenger.MessengerType);
            IWindowsController wndController = ObjectProvider.Resolve<IWindowsController>();
            if (wndController.GetIsWindowPresent(windowId))
            {
                wndController.CloseWindow(windowId);
            }
        }

        private static string GetConverationWindowID(IContact companion, MessengerType messenger)
        {
            return WindowsList.Conversation + companion.GetMessaengerContact(messenger).ID;
        }
    }
}
